<template>
  <input type="text" v-model="keyWord" />
  <h2>{{ keyWord }}</h2>
</template>

<script>
import { customRef, ref } from "vue";
export default {
  name: "App",
  setup() {
    function myRef(value, delay) {
      let timer;
      return customRef((track, trigger) => {
        return {
          get() {
            track();
            return value;
          },
          set(newValue) {
            clearTimeout(timer);
            timer = setTimeout(() => {
              value = newValue;
              trigger();
            }, delay);
          },
        };
      });
    }
    // let keyWord = ref("hello"); //使用vue提供的ref
    let keyWord = myRef("hello", 1000);
    return {
      keyWord,
    };
  },
};
</script>

<style></style>
